﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Visifire.Charts;


namespace DIST.LR.ZHJG.Controls.Chart
{
    public partial class ucVFPie : UserControl
    {

        public ucVFPie()
        {
            InitializeComponent();
            this.PieChart.Loaded += new RoutedEventHandler(PieChart_Loaded);

            #region 测试
            List<Pie2Value> dd = new List<Pie2Value>();
            dd.Add(new Pie2Value("耕地量", 50, PieChartLabelStyle.AxisXLabelPercentage));
            dd.Add(new Pie2Value("林地量", 30, PieChartLabelStyle.AxisXLabelYValue));
            dd.Add(new Pie2Value("草地量", 40, PieChartLabelStyle.Percentage));
            dd.Add(new Pie2Value("水域量", 80, PieChartLabelStyle.AxisXLabelYValue));
            Pie2SeriseSetting seting = new Pie2SeriseSetting(45);
            PieChartSetting titleSet = new PieChartSetting("用地量百分比");
            //PieChartSetting titleSet = new PieChartSetting(null);
            Pie2Info<Pie2Value> cc = new Pie2Info<Pie2Value>();
            cc.DataSource = dd;
            cc.Pie2Setting = seting;
            cc.ChartSet = titleSet;
            BinDingPie(cc); 
            #endregion
        }
        void PieChart_Loaded(object sender, RoutedEventArgs e)
        {
            var c = sender as Visifire.Charts.Chart;
            IEnumerable<TextBlock> lablels = VisualTreeAPI.FindVisualChildren<TextBlock>(c.Parent as System.Windows.Controls.Grid);
            foreach (TextBlock txt in lablels)
            {
                if (txt.Text.ToLower().Contains("trial"))
                {
                    txt.Visibility = System.Windows.Visibility.Collapsed;
                    if (txt.Parent is Border)
                    {
                        (txt.Parent as Border).Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
                    }
                }
            }
        }
        /// <summary>
        /// Chart控件数据绑定
        /// </summary>
        /// <typeparam name="T">绑定类型</typeparam>
        /// <param name="pie2info">类型实例</param>
        public void BinDingPie<T>(Pie2Info<T> pie2info)
        {           
            #region Chart初始化设置
            PieChart.Series.Clear();
            Visifire.Charts.Title title = new Title();
            title.Text = pie2info.ChartSet.ChartTitle1;
            PieChart.Titles.Add(title);
            Visifire.Charts.DataSeries dataSerise= new DataSeries(); 
            dataSerise.RenderAs = RenderAs.Pie;
            dataSerise.StartAngle = pie2info.Pie2Setting.StartAngle1;
            #endregion 

            #region 图例，未完成
            //#region 设置Legends
            //Legend legend = new Legend();
            //PieChart.Legends.Add(legend);
            //#endregion

            //#region
            //Visifire.Charts.ToolTip tip = new Visifire.Charts.ToolTip();
            //tip.VerticalAlignment = VerticalAlignment.Bottom;
            //PieChart.ToolTips.Add(tip);
            //#endregion
            //dataSerise.MarkerType = Visifire.Commons.MarkerTypes.Circle; 
            #endregion

            DataPoint dataPoint;
            foreach (Pie2Value info in pie2info.DataSource)
            {
                dataPoint = new DataPoint();
                dataPoint.AxisXLabel = info.AxisXLabelName1;
                dataPoint.YValue = info.YValue1;
                dataPoint.Style = this.Resources[info.LabelStyle1.ToString()] as Style;
                dataSerise.DataPoints.Add(dataPoint);
            }
            PieChart.Series.Add(dataSerise);       
        }
    }

    public class Pie2Info<T>
    {
       
        public Pie2Info()
        {
            this.DataSource = new List<Pie2Value>();
        }

        public Pie2SeriseSetting Pie2Setting { get; set; }
        public List<Pie2Value> DataSource { get; set; }
        public PieChartSetting ChartSet { get; set; }
    }

    #region Chart相关设置
    /// <summary>
    /// 图表设置
    /// </summary>
    public class PieChartSetting
    {
        private string ChartTitle;

        public PieChartSetting(string _chartTitle)
        {
            this.ChartTitle = _chartTitle;
        }

        public string ChartTitle1
        {
            get { return ChartTitle; }
            set
            {
                if (value != null)
                {
                    ChartTitle = value;
                }
                else
                {
                    ChartTitle = null;
                }
            }
        }
    }

    /// <summary>
    /// 图表样式设置
    /// </summary>
    public class Pie2SeriseSetting
    {
        private double StartAngle;

        public double StartAngle1
        {
            get { return StartAngle; }
            set { StartAngle = value; }
        }

        public Pie2SeriseSetting(double _startAngle)
        {
            this.StartAngle = _startAngle;
        }
    }

    /// <summary>
    /// 枚举，定义图表文字显示样式组合
    /// </summary>
    public enum PieChartLabelStyle
    {
        AxisXLabelPercentage,//文字 百分比
        AxisXLabelYValue,
        Percentage,//百分比
        XValue,//X轴数值
        YValue,//Y轴数值
        XValueYValue,//X轴数值 Y轴数值
        Null
    }

    /// <summary>
    /// 图表显示内容设置
    /// </summary>
    public class Pie2Value
    {
        private string AxisXLabelName;
        private double YValue;
        private PieChartLabelStyle LabelStyle;

        public PieChartLabelStyle LabelStyle1
        {
            get { return LabelStyle; }
            set { LabelStyle = value; }
        }

        public string AxisXLabelName1
        {
            get { return AxisXLabelName; }
            set { AxisXLabelName = value; }
        }

        public double YValue1
        {
            get { return YValue; }
            set { YValue = value; }
        }

        public Pie2Value(string _lName, double _yValue)
        {
            this.AxisXLabelName = _lName;
            this.YValue = _yValue;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_lName">AxisXLabel值</param>
        /// <param name="_yValue">Y轴数值</param>
        /// <param name="_labelStyle">文字显示组合样式</param>
        public Pie2Value(string _lName, double _yValue, PieChartLabelStyle _labelStyle)
        {
            this.AxisXLabelName = _lName;
            this.YValue = _yValue;
            this.LabelStyle = _labelStyle;
        }
    }
    #endregion
}
